cd "directory"

import delimited "solar_application.csv", clear
drop if customersector == "Residential"
drop if servicezip == .
gen date = date(appreceiveddate, "MDY")
format date %td
gen year = year(date)
gen count = 1
replace customersector = "Other" if customersector != "Commercial"&customersector != "Industrial"
collapse (sum) count systemsizedc systemsizeac, by(servicezip year customersector)

egen id = group(servicezip year)
egen id_type = group(customersector)
reshape wide count systemsizedc systemsizeac customersector, i(id) j(id_type)

ren servicezip zip
merge m:1 zip using "zip_zcta_crosswalk.dta"
drop _merge

collapse (firstnm) customersector1 customersector2 customersector3 (sum) count1 systemsizedc1 systemsizeac1 count2 systemsizedc2 systemsizeac2 count3 systemsizedc3 systemsizeac3, by(zcta year)
drop if year<2011
drop if year>2016

merge m:1 zcta year using "CA_GHI_zip.dta"
drop if _merge == 1
drop _merge

foreach var in count1 systemsizedc1 systemsizeac1 count2 systemsizedc2 systemsizeac2 count3 systemsizedc3 systemsizeac3 {
replace `var' = 0 if `var' == .
}
gen count = count1+count2+count3
gen systemsizedc = systemsizedc1+systemsizedc2+systemsizedc3
gen systemsizeac = systemsizeac1+systemsizeac2+systemsizeac3

merge m:1 zcta using "zcta_county_crosswalk.dta"
drop if _merge != 3
drop _merge
egen county_year = group(fips year)

merge 1:1 zcta year using "demo"
drop if _merge != 3
drop _merge

merge 1:1 zcta year using "income"
drop if _merge != 3
drop _merge

merge 1:1 zcta year using "edu"
drop if _merge != 3
drop _merge

cd "results"

/*Table B1*/

tsset zcta year
gen ghi_1 = L.ghi
replace ghi_1 = ghi if year == 2011

reghdfe L.count ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, replace keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Non-Residential) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.systemsizeac ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Non-Residential) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.count1 ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Commercial) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.systemsizeac1 ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Commercial) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.count2 ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Industrial) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.systemsizeac2 ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Industrial) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.count3 ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Government) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
reghdfe L.systemsizeac3 ghi_1 L.ghi_1 pop male age white highschool_more income_median income_15k income_25k income_35k income_50k income_75k income_100k income_150k income_200k income_200kmore, absorb(zcta county_year) cluster(zcta)
outreg2 using result_solar_non_residential, append keep(ghi_1 L.ghi_1) nocon label tex(frag) ct(Government) addtext(County-Year FEs, YES, ZCTA FEs, YES) nonotes
